import { tableConfig } from "@/config/tableConfig";
import router from "@/router";
import { dayjs, ElNotification } from "element-plus";
const butterLoading = ref(false);

export const timeToMs = (time: string) => {
  const date = new Date(time).getTime();
  return date;
};
export const formatTime = (time: number) => {
  return dayjs(time).format("YYYY-MM-DDTHH:mm:ss.SSSZ");
};
export const openHaiKang = async (row: any) => {
  console.log(row);

  butterLoading.value = true;
  return axios
    .post(
      "https://hik-api-proxy.cyberstone.com.cn/proxy/videoV2CamerasPlaybackURLs",
      {
        beginTime: formatTime(timeToMs(row.eventTime) - 30000),
        endTime: formatTime(timeToMs(row.eventTime) + 30000),
        pointName: row.monitorName,
        withNvr: false,
        type: "wss",
      }
    )
    .then(({ data }) => {
      const object = {};
      if (data.data && data.data.url) {
        router.push({
          name: "Video",
          query: {
            url: data.data.url,
            startTIme: formatTime(timeToMs(row.eventTime) - 30000),
            endTime: formatTime(timeToMs(row.eventTime) + 30000),
            point: row.monitorName,
          },
        });
      } else {
        ElNotification.error("当前点位无监控");
      }
    })
    .finally(() => {
      butterLoading.value = false;
    });
};

const config = tableConfig({
  column: [
    {
      prop: "monitorName",
      label: "监控地点",
      type: "select",
      filter: true,
      minWidth: "200",
    },
    {
      prop: "eventImg",
      label: "抓拍图片",
      show: false,
      showImage: true,
      minWidth: "200",
    },
    {
      prop: "eventTime",
      type: "datetimerange",
      label: "抓拍时间",
      minWidth: "200",
    },
    {
      prop: "option",
      show: false,
      label: "操作",
      minWidth: "100",
      render: (scope) => {
        return (
          <el-button
            loading={butterLoading.value}
            type="primary"
            onClick={() => openHaiKang(scope.row)}
            link
          >
            查看录像
          </el-button>
        );
      },
    },
  ],
  showIndexColumn: true,
});

export default config;
